<?php


namespace app\api\controller\v1;
use app\api\model\v1\Area;
use app\api\model\v1\EnterpriseCategory;
use app\api\model\v1\EnterpriseJob;
use app\api\model\v1\EnterpriseResume;
use app\api\model\v1\UserEnterprise;
use app\api\model\v1\UserResume;
use app\common\controller\Api;

class Enterprise extends Api
{
    protected $noNeedLogin = ['enterpriseCategory','enterpriseJobDetail','jobList','enterpriseList','enterpriseInfo','enterpriseJob'];
    protected $noNeedRight = ['*'];

    /**
     * 创业就业分类
     */
    public function enterpriseCategory()
    {
        $list = EnterpriseCategory::order('weigh asc')->field('id,name')->select();
        $this->success('成功', $list);
    }

    /**
     * 增加/编辑招聘信息
     */
    public function jobCreate()
    {
        $name = $this->request->post('name'); //岗位名称
        $experience = $this->request->post('experience') ?? ''; //经验要求
        $education = $this->request->post('education') ?? ''; //学历要求
        $occupation = $this->request->post('occupation') ?? ''; //职业要求
        $address = $this->request->post('address'); //工作地址
        $desc = $this->request->post('desc') ?? ''; //岗位描述
        $username = $this->request->post('username'); //联系人
        $mobile = $this->request->post('mobile'); //联系方式
        $enterprise_category_id = $this->request->post('enterprise_category_id'); //所属分类
        $province_name = $this->request->post('province_name'); //省
        $city_name = $this->request->post('city_name'); //市
        $district_name = $this->request->post('district_name'); //区
        $province_id = $this->request->post('province_id') ?? 0; //省id
        $city_id = $this->request->post('city_id') ?? 0; //市id
        $district_id = $this->request->post('district_id') ?? 0; //区id
        $price_min = $this->request->post('price_min'); //最低薪资
        $price_max = $this->request->post('price_max'); //最高薪资
        $is_full = $this->request->post('is_full') ?? 0; //是否招满

        $id = $this->request->post('id');
        if (!trim($name)) $this->error('请输入岗位名称');
        if (!trim($desc)) $this->error('请输入岗位描述');
        if (!trim($username)) $this->error('请输入联系人');
        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error('请输入联系方式');
        }
        if (!EnterpriseCategory::where('id', $enterprise_category_id)->find()) {
            $this->error('请选择职业分类');
        }
        if (!$province_name || !$city_name || !$district_name) {
            $this->error('请选择工作地址');
        }
        if (!isset($price_min) || !isset($price_max)) {
            $this->error('请输入工作薪资');
        }
        $userModel = $this->auth->getUser();
        if ($userModel->identity != 'enterprise') $this->error('没有权限！');
        if ($id) {
            $model = EnterpriseJob::where('user_id', $userModel->id)->where('id', $id)->find();
            if (!$model) $this->error('招聘信息不存在');
        } else {
            $model = new EnterpriseJob();
        }
        $model->user_id = $userModel->id;
        $model->name = $name;
        if ($experience) {
            $model->experience = $experience;
        }
        if ($education) {
            $model->education = $education;
        }
        if ($occupation) {
            $model->occupation = $occupation;
        }
        if ($province_id) {
            $model->province_id = $province_id;
        }
        if ($city_id) {
            $model->city_id = $city_id;
        }
        if ($district_id) {
           $model->district_id = $district_id;
        }
        $model->desc = $desc;
        $model->username = $username;
        $model->mobile = $mobile;
        $model->enterprise_category_id = $enterprise_category_id;
        $model->province_name = $province_name;
        $model->city_name = $city_name;
        $model->district_name = $district_name;
        $model->is_full = $is_full;
        $model->price_min = $price_min;
        $model->price_max = $price_max;
        $model->user_enterprise_id = $userModel->identity_id;
        $model->save();
        $this->success('成功');
    }

    /**
     * 企业招聘-发布者列表
     */
    public function enterpriseJobList()
    {
        $limit = $this->request->post('limit') ?? 10;
        $userModel = $this->auth->getUser();
        if ($userModel->identity != 'enterprise') $this->error('没有权限！');
        $map = [];
        $map['j.user_id'] = ['=', $userModel->id];
        $map['ue.id'] = ['=', $userModel->identity_id];
        $list = EnterpriseJob::where($map)
            ->alias('j')
            ->join('__USER_ENTERPRISE__ ue', 'j.user_id = ue.user_id')
            ->field('j.*,ue.name as enterprise_name')
            ->order('j.createtime desc')
            ->paginate($limit)
            ->toArray();
//        if (!empty($list['data'])) {
//            foreach ($list['data'] as $key => $value) {
//                $list['data'][$key]['city_name'] = Area::where('id', $value['city_id'])->value('shortname');
//            }
//        }
        $this->success('成功', $list);
    }

    /**
     * 招聘信息详情
     */
    public function enterpriseJobDetail()
    {
        $id = $this->request->post('id');
        $res = EnterpriseJob::where('id', $id)->find();
//        if ($res) {
//            $res->province_name = Area::where('id', $res->province_id)->value('name');
//            $res->city_name = Area::where('id', $res->city_id)->value('name');
//            $res->district_name = Area::where('id', $res->district_id)->value('name');
//        }
        $this->success('成功', $res);
    }

    /**
     * 招聘信息删除
     */
    public function enterpriseJobDelete()
    {
        $id = $this->request->post('id');
        $userModel = $this->auth->getUser();
        EnterpriseJob::where('id', $id)->where('user_id', $userModel->id)->delete();
        $this->success('成功');
    }

    /**
     * 我要求职
     */
    public function enterpriseResume()
    {
//        $data = [
//            [
//                'company' => '郑州威风科技',
//                'time' => '2019.6-2023.8',
//                'content' => '销售',
//            ]
//        ];
//        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
//        halt($data);
        $userModel = $this->auth->getUser();
        $avatar = $this->request->post('avatar'); //头像
        $username = $this->request->post('username'); //姓名
        $mobile = $this->request->post('mobile'); //联系方式
        $email = $this->request->post('email'); //邮箱
        $desc = $this->request->post('desc'); //自我评价
        $education_images = $this->request->post('education_images'); //学历证书
        $education_experience = $this->request->post('education_experience'); //教育经历
        $resume = $this->request->post('resume'); //工作经历
        $education = $this->request->post('education'); //学历
        $education_experience = html_entity_decode($education_experience);
        $resume = html_entity_decode($resume);
        if (!trim($username)) $this->error('请输入姓名');
        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error('请输入联系方式');
        }
        if (!trim($desc)) $this->error('请输入自我评价');
        if (!trim($education_images)) $this->error('请上传学历证书');
//        if (!trim($education)) $this->error('请输入学历');
        if (!trim($education_experience)) $this->error('请输入教育经历');
        if (!trim($resume)) $this->error('请输入工作经历');
        $model = EnterpriseResume::where('user_id', $userModel->id)->find();
        if (!$model) {
            $model = new EnterpriseResume();
        }
        $model->user_id = $userModel->id;
        $model->avatar = $avatar;
        $model->username = $username;
        $model->mobile = $mobile;
        $model->email = $email;
        $model->desc = $desc;
        $model->education_images = trim($education_images, ',');
        $model->education_experience = $education_experience;
        $model->resume = $resume;
        if ($education) {
            $model->education = $education;
        }
        $model->save();
        $this->success('成功');
    }

    /**
     * 我要求职-详情
     */
    public function enterpriseResumeDetail()
    {
        $userModel = $this->auth->getUser();
        $model = EnterpriseResume::where('user_id', $userModel->id)->find();
        $this->success('成功', $model);
    }

    /**
     * 投递简历
     */
    public function submitResume()
    {
        $userModel = $this->auth->getUser();
        $job_id = $this->request->post('job_id'); //招聘ID
        $jobModel = EnterpriseJob::where('id', $job_id)->find();
        if (!$jobModel) $this->error('招聘信息不存在');
        if ($jobModel->is_full == 1) $this->error('该信息已招满');
        $resumeModel = EnterpriseResume::where('user_id', $userModel->id)->find();
        if (!$resumeModel) $this->error('请先设置求职简历');
        $userResumeModel = UserResume::where('user_id', $userModel->id)->where('enterprise_job_id', $job_id)->find();
        if (!$userResumeModel) {
            $userResumeModel = new UserResume();
        }
        $userResumeModel->user_id = $userModel->id;
        $userResumeModel->enterprise_resume_id = $resumeModel->id;
        $userResumeModel->enterprise_job_id = $job_id;
        $userResumeModel->avatar = $resumeModel->avatar;
        $userResumeModel->username = $resumeModel->username;
        $userResumeModel->mobile = $resumeModel->mobile;
        $userResumeModel->email = $resumeModel->email;
        $userResumeModel->desc = $resumeModel->desc;
        $userResumeModel->education_images = $resumeModel->education_images;
        $userResumeModel->education_experience = $resumeModel->education_experience;
        $userResumeModel->resume = $resumeModel->resume;
        $userResumeModel->education = $resumeModel->education;
        $userResumeModel->save();
        $this->success('成功');
    }

    /**
     * 求职简历-申请列表
     */
    public function userResumeList()
    {
        $limit = $this->request->post('limit') ?? 10;
        $userModel = $this->auth->getUser();
        $job_id = $this->request->post('job_id'); //招聘ID
        $jobModel = EnterpriseJob::where('id', $job_id)->where('user_id', $userModel->id)->find();
        if (!$jobModel) {
            $list = ['total'=>0,'per_page'=>$limit,'current_page'=>1,'last_page'=>0,'data'=>[]];
            $this->success('成功', $list);
        }
        $list = UserResume::where('enterprise_job_id', $job_id)->order('id desc')->paginate($limit)->toArray();
        $this->success('成功', $list);
    }

    /**
     * 求职简历-详情
     */
    public function userResumeDetail()
    {
        $userModel = $this->auth->getUser();
        $id = $this->request->post('id'); //申请ID
        $userResumeModel = UserResume::where('id', $id)->find();
        if (!$userResumeModel) $this->error('简历不存在');
        $jobModel = EnterpriseJob::where('id', $userResumeModel->enterprise_job_id)->find();
        if ($jobModel && $jobModel->user_id != $userModel->id) {
            $this->error('没有权限');
        }
        $this->success('成功', $userResumeModel);
    }

    /**
     * 求职简历-删除
     */
    public function userResumeDelete()
    {
        $userModel = $this->auth->getUser();
        $job_id = $this->request->post('job_id'); //招聘ID
        $ids = $this->request->post('ids'); //要删除的IDS
        $ids = trim($ids, ',');
        $ids = explode(',', $ids);
        $jobModel = EnterpriseJob::where('id', $job_id)->where('user_id', $userModel->id)->find();
        if (!$jobModel) $this->error('招聘信息不存在');
        UserResume::where('id', 'in', $ids)->where('enterprise_job_id', $job_id)->delete();
        $this->success('成功');
    }

    /**
     * 创业就业-根据分类查询职位
     */
    public function jobList()
    {
        $limit = $this->request->post('limit') ?? 10;
        $category_id = $this->request->post('category_id'); //分类ID
        $province_name = $this->request->post('province_name'); //省
        $city_name = $this->request->post('city_name'); //市
        $district_name = $this->request->post('district_name'); //区
        $map = [];
        if ($category_id) {
            $map['enterprise_category_id'] = ['=', $category_id];
        }
        if ($province_name) {
            $map['province_name'] = ['=', $province_name];
        }
        if ($city_name) {
            $map['city_name'] = ['=', $city_name];
        }
        if ($district_name) {
            $map['district_name'] = ['=', $district_name];
        }
        $list = EnterpriseJob::where($map)->order('id desc')->paginate($limit)->toArray();
        foreach ($list['data'] as $key => $value) {
            $list['data'][$key]['enterprise_name'] = UserEnterprise::where('id', $value['user_enterprise_id'])->find()['name'] ?? '';
        }
        $this->success('成功', $list);
    }
    
    /**
     * 企业列表
     */
    public function enterpriseList()
    {
        $limit = $this->request->post('limit') ?? 10;
        $category = $this->request->post('category');
        $map = [];
        $map['status'] = ['=', 1];
        if ($category) {
            $map['category'] = ['=', $category];
        }
        $list = UserEnterprise::where($map)->order('id desc')->paginate($limit)->toArray();
        $this->success('成功', $list);
    }

    /**
     * 企业详情
     */
    public function enterpriseInfo()
    {
        $id = $this->request->post('id'); //企业ID
        $result = UserEnterprise::where('id', $id)->where('status', 1)->find();
        $this->success('成功', $result);
    }
    
    /**
     * 企业招聘信息列表
     */
    public function enterpriseJob()
    {
        $id = $this->request->post('id'); //企业ID
        $limit = $this->request->post('limit') ?? 10;
        $map = [];
        $map['j.user_enterprise_id'] = ['=', $id];
        $list = EnterpriseJob::where($map)
            ->alias('j')
            ->join('__USER_ENTERPRISE__ ue', 'j.user_id = ue.user_id')
            ->field('j.*,ue.name as enterprise_name')
            ->order('j.createtime desc')
            ->paginate($limit)
            ->toArray();
//        if (!empty($list['data'])) {
//            foreach ($list['data'] as $key => $value) {
//                $list['data'][$key]['city_name'] = Area::where('id', $value['city_id'])->value('shortname');
//            }
//        }
        $this->success('成功', $list);
    }
}